import { message } from 'antd';
import { list, assign, listSpecial, listClassroom, listDormitory } from '../services/assign';

export default {
  namespace: 'assign',

  state: {
    data: [],
    total: 0,
    success: true,
    dataSpecial: [], // 开设专业数据
    dataClassroom: [], // 班级数据
    dataDormitory: [], // 宿舍数据
  },

  effects: {
    *list({ payload }, { call, put }) {
      const response = yield call(list, payload);
      yield put({ type: 'callbackList', response });
    },
    *assign({ payload }, { call, put }) {
      const response = yield call(assign, payload);
      yield put({ type: 'callbackSave', response });
    },
    *listSpecial({ payload }, { call, put }) {
      const response = yield call(listSpecial, payload);
      yield put({ type: 'callbackListSpecial', response });
    },
    *listClassroom({ payload }, { call, put }) {
      const response = yield call(listClassroom, payload);
      yield put({ type: 'callbackListClassroom', response });
    },
    *listDormitory({ payload }, { call, put }) {
      const response = yield call(listDormitory, payload);
      yield put({ type: 'callbackListDormitory', response });
    },
  },

  reducers: {
    callbackList(st, { response }) {
      const state = st;
      if (response.success) {
        state.total = response.total;
        state.data = response.root;
      }
      return { ...state };
    },
    callbackSave(st, { response }) {
      const state = st;
      state.success = response.success;
      if (response.success) message.success(response.msg);
      return { ...state };
    },
    callbackListSpecial(st, { response }) {
      const state = st;
      if (response.success) {
        state.dataSpecial = response.root
          .sort((a, b) => a.id - b.id)
          .sort((a, b) => b.sequ - a.sequ)
          .map((item) => {
            const { id, name } = item;
            return { key: id, title: name };
          });
      }
      return { ...state };
    },
    callbackListClassroom(st, { response }) {
      const state = st;
      if (response.success) {
        state.dataClassroom = response.root
          .sort((a, b) => a.id - b.id)
          .sort((a, b) => a.specialId - b.specialId);
      }
      return { ...state };
    },
    callbackListDormitory(st, { response }) {
      const state = st;
      if (response.success)
        state.dataDormitory = response.root.sort((a, b) => a.id - b.id);
      return { ...state };
    },
  },
};
